package top.duduwy;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

// ConditionalOnProperty注解，添加属性条件
// @EnableConfigurationProperties指定装配的属性Bean
@EnableConfigurationProperties(CustomProperties.class)
public class exp02Config {
    @Bean
    @ConditionalOnProperty(prefix = "dudu.auto", name = "enable", havingValue = "true")
    CommandLineRunner createCustomCommandLineRunner(Environment environment) {
        return new CustomCommandLineRunner(environment);
    }

    // 自定义事件发布器
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster customApplicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);

        return eventMulticaster;
    }
}
